class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public class Test {
    public static void main(String[] a) {

    }
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if(l1 == null) return l2;
        if(l2 == null) return l1;
        ListNode node = new ListNode();
        ListNode cur = node;
        int many = 0;
        while(l1 != null && l2 != null) {
            int tmp = l1.val + l2.val + many;
            ListNode ret = new ListNode(tmp%10);
            many = tmp/10;
            cur.next = ret;

            cur = cur.next;
            l1 = l1.next;
            l2 = l2.next;
        }
        while(l1 != null) {
            int tmp = l1.val + many;
            ListNode ret = new ListNode(tmp%10);
            many = tmp/10;
            cur.next = ret;

            cur = cur.next;
            l1 = l1.next;
        }
        while(l2 != null) {
            int tmp = l2.val + many;
            ListNode ret = new ListNode(tmp%10);
            many = tmp/10;
            cur.next = ret;

            cur = cur.next;
            l2 = l2.next;
        }
        if(many != 0) {
            ListNode ret = new ListNode(many);
            cur.next = ret;

        }
        return node.next;
    }



}
